package com.lt.integration.jpa.validation;

import javax.validation.Constraint;
import javax.validation.Payload;
import java.lang.annotation.*;

/**
 *
 * @Conditional(selected = "locationOption", values = {"SFTP", "FTP"}, required = {"host", "user", "password", "port"})
 * @Conditional(selected = "locationOption", values = {"EMAIL"}, required = {"emailAddress"})
 * public class ExportLocationDTO {
 *
 *     @NotEmpty
 *     private String name;
 *     @NotNull
 *     private ExportLocationOption locationOption;
 *     private String emailAddress;
 *     private String host;
 *     private Integer port;
 *     private String user;
 *     private String password;
 * }
 *
 * 当locationOption为EMAIL时 emailAddress必填
 *
 *
 * 验证某一字段为某一值时，其他字段不为空
 * @author: xiaobin
 * @create: 2020-05-12 15:23
 */
@Repeatable(Conditionals.class)
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = {ConditionalValidator.class})
public @interface Conditional {

    String message() default "This field is required.";
    Class<?>[] groups() default {};
    Class<? extends Payload>[] payload() default {};

    /**
     * 选择的字段
     * 当selected的值包含values中的一个值时，required中的字段必须存在
     * @return
     */
    String selected();
    String[] required();
    String[] values() default {};
}
